#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, char* argv[])
{
    if(argc < 2) {
        printf("errore\n\r");
        return 0;
    }

    if(!strcmp(argv[1], "home")) {
        
        if(argc < 4) {
            printf("errore\n\r");
            return 0;
        }
        
        char home[256];
        char cmd[256];
        
        snprintf(home, sizeof(home), "/home/%s", argv[2]);
        
        snprintf(cmd, sizeof(cmd), "/bin/mkdir %s", home);
        
        if(system(cmd) == -1) {
            perror("system error");
            return 0;
        }

        snprintf(cmd, sizeof(cmd), "cp /etc/skel/.* %s/", home);
        
        if(system(cmd) == -1) {
            perror("system error");
            return 0;
        }
        
        snprintf(cmd, sizeof(cmd), "/bin/chown -R %s:%s %s", argv[3], argv[4], home);

        if(system(cmd) == -1) {
            perror("system error");
            return 0;
        }
        
        snprintf(cmd, sizeof(cmd), "/bin/chmod 750 %s", home);

        if(system(cmd) == -1) {
            perror("system error");
            return 0;
        }

        
        return 0;
    } else if(!strcmp(argv[1], "dir")) {
        
        if(argc < 5) {
            printf("errore\n\r");
            return 0;
        }
        
        char dir[256];
        char cmd[256];
        
        snprintf(dir, sizeof(dir), "/home/%s%s", argv[2], argv[3]);
        
        snprintf(cmd, sizeof(cmd), "/bin/mkdir %s", dir);

        if(system(cmd) == -1) {
            perror("system error");
            return 0;
        }
        
        snprintf(cmd, sizeof(cmd), "/bin/chown %s:%s %s", argv[4], argv[5], dir);        

        if(system(cmd) == -1) {
            perror("system error");
            return 0;
        }
        
        return 0;
        }
    
    return 0;
}
